package com.example.taobao

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageButton
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import java.net.URL

class weitaoAdapter(val content : Context, val weitaoList: List<weitaoData>) : RecyclerView.Adapter<weitaoAdapter.ViewHoilder>() {

    private lateinit var onItemClickListener: OnItemClickListener



   fun setOnItemClickListener(listener: OnItemClickListener) {
        this.onItemClickListener = listener
   }



    inner class ViewHoilder(view:View) : RecyclerView.ViewHolder(view){

        val shopname:TextView = view.findViewById(R.id.shop_name)
        val shop_desceibe : TextView = view.findViewById(R.id.someD)
        val fans_num : TextView = view.findViewById(R.id.fans_number)
        val image1 : ImageView = view.findViewById(R.id.p1)
        val image2 : ImageView = view.findViewById(R.id.p2)
        val image3 : ImageView = view.findViewById(R.id.p3)
        val read_num:TextView = view.findViewById(R.id.read_num)
        val like_num: TextView = view.findViewById(R.id.like_number)
        val like_image : ImageButton = view.findViewById(R.id.like)
        val messages:TextView = view.findViewById(R.id.message)
        val gos : ImageButton = view.findViewById(R.id.go_main)
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHoilder {
        val view = LayoutInflater.from(content).inflate(R.layout.wei_item, parent, false)

        return ViewHoilder(view)

    }



    override fun getItemCount(): Int {
        return weitaoList.size
    }

    override fun onBindViewHolder(holder: ViewHoilder, position: Int) {
        val wei = weitaoList[position]
        holder.shopname.text = wei.shopname
        holder.shop_desceibe.text = wei.shopD
        holder.fans_num.text = wei.fans_num.toString() + "万粉丝"
      //  holder.image1.setImageResource(wei.picture1)
        //holder.image2.setImageResource(wei.picture2)
       // holder.image3.setImageResource(wei.picture3)
        holder.read_num.text= wei.read_nums.toString()+ "阅读"
        holder.like_num.text = wei.like_num.toString()
        holder.messages.text= wei.mess
        Glide.with(content).load(wei.picture1).into(holder.image1)
        Glide.with(content).load(wei.picture2).into(holder.image2)
        Glide.with(content).load(wei.picture3).into(holder.image3)

        holder.like_image.setOnClickListener {
            onItemClickListener?.onItemClick(holder.itemView, position, 0)
            holder.like_image.setImageResource(R.drawable.liked)
            holder.like_num.text = (holder.like_num.text.toString().toInt()+1).toString()
        }

        holder.read_num.setOnClickListener {
            onItemClickListener?.onItemClick(holder.itemView, position, 0)
        }

        holder.gos.setOnClickListener {
            onItemClickListener?.onItemClick(holder.itemView, position, 1)
        }

    }

    interface OnItemClickListener{
        fun onItemClick(view: View, position: Int, type : Int)
    }


}